package com.medicine.controller.admin;

import com.medicine.dto.GroupDTO;
import com.medicine.dto.GroupPageQueryDTO;
import com.medicine.result.PageResult;
import com.medicine.result.Result;
import com.medicine.service.GroupService;
import com.medicine.vo.GroupVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 药组管理
 */
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "药组相关接口")
@Slf4j
public class GroupController {

    @Autowired
    private GroupService groupService;

    /**
     * 新增药组
     * @param groupDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增药组")
    @CacheEvict(cacheNames = "groupCache",key = "#groupDTO.categoryId")//key: groupCache::100
    public Result save(@RequestBody GroupDTO groupDTO) {
        groupService.saveWithMedicine(groupDTO);
        return Result.success();
    }

    /**
     * 分页查询
     * @param groupPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(GroupPageQueryDTO groupPageQueryDTO) {
        PageResult pageResult = groupService.pageQuery(groupPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除药组
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除药组")
    @CacheEvict(cacheNames = "groupCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        groupService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 根据id查询药组，用于修改页面回显数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询药组")
    public Result<GroupVO> getById(@PathVariable Long id) {
        GroupVO groupVO = groupService.getByIdWithMedicine(id);
        return Result.success(groupVO);
    }

    /**
     * 修改药组
     *
     * @param groupDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改药组")
    @CacheEvict(cacheNames = "groupCache",allEntries = true)
    public Result update(@RequestBody GroupDTO groupDTO) {
        log.info("修改药组：{}", groupDTO);
        groupService.update(groupDTO);
        return Result.success();
    }

    /**
     * 药组起售停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("药组起售停售")
    @CacheEvict(cacheNames = "groupCache",allEntries = true)
    public Result startOrStop(@PathVariable Integer status, Long id) {
        groupService.startOrStop(status, id);
        return Result.success();
    }
}